package com.yxw.yxnet_cd_center.customer_service.service.impl;




import java.util.List;
import java.util.ArrayList;
import java.util.UUID;
import java.io.File;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.yxw.yxnet_cd_center.common.constant.Constants;
import javax.servlet.http.HttpServletRequest;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import com.yxw.yxnet_cd_center.customer_service.utils.Aes128;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;
import com.yxw.yxnet_cd_center.common.constant.MqttPrefixConstant;
import com.yxw.yxnet_cd_center.common.enums.ContentTypeEnum;

import com.yxw.yxnet_cd_center.customer_service.mapper.YxcscClientFiledServiceMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.YxcscCustomerAccessServiceMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.YxcscMqttServersClientMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.YxcscServiceTalkRecordMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.FlockMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.TechFlockUserMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.YxcscFlockTalkRecordMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.UserMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.WorkflowMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.CustomerToFlockMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.YxCsFlockUserMapper;
import com.yxw.yxnet_cd_center.customer_service.mapper.CustomerFlockUserMapper;
        
import com.yxw.yxnet_cd_center.customer_service.entity.YxcscCustomerAccessServiceEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxcscClientFiledServiceEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxcscMqttServersClientEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxcscServiceTalkRecordEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsUserEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.CustomerToFlockEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsFlockEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.TechFlockUserEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsWorkflowEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsFlockUserEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.YxcscFlockTalkRecordEntity;
import com.yxw.yxnet_cd_center.customer_service.entity.CustomerFlockUserEntity;
import com.yxw.yxnet_cd_center.customer_service.vo.MqttServerInfor2JsVO;
import com.yxw.yxnet_cd_center.customer_service.vo.MqttServerUserToFlockVO;
import com.yxw.yxnet_cd_center.customer_service.vo.MqttTechFlockTopicVO;

import com.yxw.yxnet_cd_center.customer_service.dto.CustomQueryServiceDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.CustomQueryWorkflowDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.YxcscServiceTalkRecordDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.CustomerSetWorkFlowDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.TechnicalSupportFinishWorkFlowDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.CustomerRequestServiceDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.YxcscFlockTalkRecordDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.SessionTalkRecordDTO;

import com.yxw.yxnet_cd_center.customer_service.service.IYxcscCustomerAccessServiceService;
import com.yxw.yxnet_cd_center.customer_service.service.IMsgProcessService;
import com.yxw.yxnet_cd_center.customer_service.vo.MemberVo;
import java.util.Objects;
import com.yxw.yxnet_cd_center.customer_service.utils.UserUtil;
import java.io.UnsupportedEncodingException;
/*

import com.yxw.customer_service_center.customer_service.mapper.UserWorkMapper;
import com.yxw.customer_service_center.customer_service.utils.RedisManager;
import com.yxw.customer_service_center.customer_service.dto.YxcscCustomerAccessServiceDTO;
import java.util.ArrayList;
import com.yxw.customer_service_center.customer_service.entity.YxCsUserWorkEntity;
import com.yxw.customer_service_center.customer_service.vo.MqttServerInfor2AndroidVO;
import com.yxw.customer_service_center.customer_service.vo.MqttServersListenerTypeVO;
import com.yxw.yxnet_cd_center.common.constant.RedisIdConstant;
import com.yxw.customer_service_center.customer_service.mapper.GroupMapper;
import com.yxw.customer_service_center.customer_service.mapper.FlockMapper;
import com.yxw.customer_service_center.customer_service.dto.ClientFiledDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.util.ObjectUtil;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
*/

/**
 * 客服会话所有处理
 * @author hjx
 * @date 2023.09.22
 */
@Service
//public class YxcscCustomerAccessServiceServiceImpl  extends ServiceImpl<YxcscCustomerAccessServiceMapper, YxcscCustomerAccessServiceEntity> implements IYxcscCustomerAccessServiceService{
public class YxcscCustomerAccessServiceServiceImpl  implements IYxcscCustomerAccessServiceService{
    
    @Autowired
    private YxcscCustomerAccessServiceMapper oYxcscCustomerAccessServiceMapper;
    @Autowired
    private YxcscClientFiledServiceMapper oYxcscClientFiledServiceMapper;
    @Autowired
    private IMsgProcessService oIMqttProcessService;
    @Autowired
    private YxcscServiceTalkRecordMapper oYxcscServiceTalkRecordMapper;
    @Autowired
    private YxcscMqttServersClientMapper oYxcscMqttServersClientMapper;
    @Autowired
    private YxCsFlockUserMapper oYxCsFlockUserMapper;
    @Autowired
    private CustomerToFlockMapper oCustomerToFlockMapper;
    //@Autowired
    //private RedisManager oRedisManager;
    @Autowired
    private YxcscFlockTalkRecordMapper oYxcscFlockTalkRecordMapper;
    
    @Autowired
    private UserMapper oUserMapper;
    @Autowired
    private WorkflowMapper workflowMapper;    
    @Autowired
    private FlockMapper flockMapper;   
    @Autowired
    private TechFlockUserMapper oTechFlockUserMapper;    
    @Autowired
    private CustomerFlockUserMapper oCustomerFlockUserMapper;
    @Autowired
    private UserUtil oUserUtil;
    private MqttServerInfor2JsVO getMqttServerInfor2JsVO(String channelCode,Long userId)
    {
        QueryWrapper<YxcscMqttServersClientEntity> oYxcscMqttServersClientQueryWrapper = new QueryWrapper();
        oYxcscMqttServersClientQueryWrapper.eq("channel_code",channelCode).ne("type", "tcp").orderByAsc("host");
        List<YxcscMqttServersClientEntity> oListYxcscMqttServersClient = oYxcscMqttServersClientMapper.selectList(oYxcscMqttServersClientQueryWrapper);
        if(!oListYxcscMqttServersClient.isEmpty())
        { 
            String talkTopic = "";
            //String flocks = "";
            ArrayList<MqttServerUserToFlockVO> listFlock = new ArrayList();
            QueryWrapper<CustomerToFlockEntity> oYxCsFlockUserQueryWrapper = new QueryWrapper();
            oYxCsFlockUserQueryWrapper.eq("user_id",userId);
            List<CustomerToFlockEntity> oListCustomerToFlockEntity = oCustomerToFlockMapper.selectList(oYxCsFlockUserQueryWrapper);
            if(!oListCustomerToFlockEntity.isEmpty())
            {
                for(CustomerToFlockEntity oCustomerToFlockEntity : oListCustomerToFlockEntity)
                {
                    MqttServerUserToFlockVO oMqttServerUserToFlockVO = new MqttServerUserToFlockVO();
                    if(talkTopic.equals(""))
                    {
                        talkTopic = MqttPrefixConstant.SYSTEM_FLOCK_MQTT_TOPIC_PREFIX.concat(oCustomerToFlockEntity.getFlockId().toString());
                    }
                    else
                    {
                        talkTopic = talkTopic + "," + MqttPrefixConstant.SYSTEM_FLOCK_MQTT_TOPIC_PREFIX.concat(oCustomerToFlockEntity.getFlockId().toString());
                    }
                    oMqttServerUserToFlockVO.setFlockId(oCustomerToFlockEntity.getFlockId().toString());
                    oMqttServerUserToFlockVO.setFlockName(oCustomerToFlockEntity.getFlockName());
                    oMqttServerUserToFlockVO.setTopic(MqttPrefixConstant.SYSTEM_FLOCK_MQTT_TOPIC_PREFIX.concat(oCustomerToFlockEntity.getFlockId().toString()));
                    listFlock.add(oMqttServerUserToFlockVO);
                }
            }
            YxcscMqttServersClientEntity oYxcscMqttServersClientEntity = oListYxcscMqttServersClient.get(0);
            MqttServerInfor2JsVO oMqttServerInfor2JsVO = new MqttServerInfor2JsVO();
            oMqttServerInfor2JsVO.setHostURL(oYxcscMqttServersClientEntity.getHost());
            oMqttServerInfor2JsVO.setUserName(oYxcscMqttServersClientEntity.getUser());
            oMqttServerInfor2JsVO.setPassWord(oYxcscMqttServersClientEntity.getPwd());
            oMqttServerInfor2JsVO.setClientId(channelCode + "_" + userId + "_" + java.util.UUID.randomUUID().toString().replaceAll("-", ""));                            
            oMqttServerInfor2JsVO.setType(oYxcscMqttServersClientEntity.getType());
            oMqttServerInfor2JsVO.setPort(oYxcscMqttServersClientEntity.getPort().toString());
            oMqttServerInfor2JsVO.setGroupTalkTopic(talkTopic);
            oMqttServerInfor2JsVO.setFlocks(listFlock);
            talkTopic = MqttPrefixConstant.SYSTEM_USER_MQTT_TOPIC_PREFIX.concat(userId.toString());
            oMqttServerInfor2JsVO.setClientTalkTopic(talkTopic);
            talkTopic = MqttPrefixConstant.SYSTEM_SYSTEM_MQTT_TOPIC_PREFIX.concat(userId.toString()); 
            oMqttServerInfor2JsVO.setSystemTopic(talkTopic);
            MqttTechFlockTopicVO oMqttTechFlockTopicVO = new MqttTechFlockTopicVO();
            oMqttTechFlockTopicVO.setFlockId(Constants.defaultTechFlockId.toString());
            oMqttTechFlockTopicVO.setFlockName("工单群");
            oMqttTechFlockTopicVO.setTopic(MqttPrefixConstant.SYSTEM_TECHFLOCK_MQTT_TOPIC_PREFIX.concat(Constants.defaultTechFlockId.toString()));
            oMqttServerInfor2JsVO.setTechFlock(oMqttTechFlockTopicVO);
            return oMqttServerInfor2JsVO;
        }
        return null;
    }
    /*** 
    * 不安全的获取Mqtt服务器信息
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    public BaseResult getMqttInfoNotSafe(String channelCode,Long userId)
    {
        Long tokenUserId = oUserUtil.getLoginUserId();
        if(null != tokenUserId)
        {
            if(!tokenUserId.equals(userId))
            {
                return BaseResult.failed("用户Id和登录用户Id不匹配");
            }
        }
        MqttServerInfor2JsVO oMqttServerInfor2JsVO = getMqttServerInfor2JsVO(channelCode,userId);
        if(null != oMqttServerInfor2JsVO)
        {
            
            return BaseResult.success(oMqttServerInfor2JsVO);
        }
        else
        {
            return BaseResult.failed("还没有配置MQTT服务器参数");
        }
    }
    /*** 
    * 获取Mqtt服务器信息
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    public BaseResult getMqttInfo(HttpServletRequest httpRequest,String channelCode,Long userId)
    {
        /*
        List<String> tokens  = httpRequest.getHeaders().get("token");
        String token = null;
        if(!tokens.isEmpty())
        {
            token = tokens.get(0).trim().substring(0, 16);            
        }
        else
        {
            return BaseResult.failed("没有获取到token");
        } */
        Long tokenUserId = oUserUtil.getLoginUserId();
        if(null != tokenUserId)
        {
            if(!tokenUserId.equals(userId))
            {
                return BaseResult.failed("用户Id和登录用户Id不匹配");
            }
        }        
        String token = httpRequest.getHeader("token");
        if(StrUtil.isBlank(token))
        {
            return BaseResult.failed("没有获取到token");
        }
        else
        {
            token = token.substring(0, 16);
        }
        MqttServerInfor2JsVO oMqttServerInfor2JsVO = getMqttServerInfor2JsVO(channelCode,userId);
        if(null != oMqttServerInfor2JsVO)
        {            
            JSONObject oJSONObject = (JSONObject)JSON.toJSON(oMqttServerInfor2JsVO);
            String cMatter = oJSONObject.toString();
            cMatter = Aes128.Aes128EncryptUtf8(token, cMatter).getData().toString();
            return BaseResult.success(cMatter);
        }
        else
        {
            return BaseResult.failed("还没有配置MQTT服务器参数");
        }        
    }
    /*** 
    * JS客服端完成MQTT主题订阅知会服务器接口
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    public BaseResult jsClientSubscribeTopicResult(Long userId,Integer result)
    {
        /*
        QueryWrapper<YxCsUserEntity> oYxCsUserEntityQueryWrapper = new QueryWrapper();
        oYxCsUserEntityQueryWrapper.eq("user_id",userId);
        List<YxCsUserEntity> oListYxcscMqttServersClient = oUserMapper.selectList(oYxCsUserEntityQueryWrapper);
        YxCsUserEntity oYxCsUserEntity = null;
        if(oListYxcscMqttServersClient.isEmpty())
        {
            return BaseResult.failed("找不到对应的系统用户");
        }
        oYxCsUserEntity = oListYxcscMqttServersClient.get(0);
        oYxCsUserEntity.setSubscribeTopicResult(result);
        oUserMapper.updateById(oYxCsUserEntity);
        return BaseResult.success();
        */
        Long tokenUserId = oUserUtil.getLoginUserId();
        if(null != tokenUserId)
        {
            if(!tokenUserId.equals(userId))
            {
                return BaseResult.failed("用户Id和登录用户Id不匹配");
            }
        }        
        UpdateWrapper<YxCsUserEntity> oUpdateWrapper = new UpdateWrapper();
        oUpdateWrapper.eq("user_id", userId);
        oUpdateWrapper.set("subscribe_topic_result", result);
        int i = oUserMapper.update(null, oUpdateWrapper);
        if(i == 1)
        {
            return BaseResult.success();
        }
        else
        {
            return BaseResult.failed("知会失败");
        }
    }
    /*** 
    * 客服申请某个服务
    * @param userId
    * @return MqttServerInforVO
    **/   
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult customerRequestService(CustomerRequestServiceDTO oCustomerRequestServiceDTO)
    {
        Long tokenUserId = oUserUtil.getLoginUserId();
        if(null != tokenUserId)
        {
            if(!tokenUserId.equals(oCustomerRequestServiceDTO.getUserId()))
            {
                return BaseResult.failed("用户Id和登录用户Id不匹配");
            }
        }        
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(oCustomerRequestServiceDTO.getSessionId());
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        }
        if(null==oYxcscClientFiledServiceEntity.getAccessNumber())
        {
            oYxcscClientFiledServiceEntity.setAccessNumber(0);
        }
        YxCsUserEntity oYxCsUserEntity = oUserMapper.selectById(oCustomerRequestServiceDTO.getUserId());
        if(null == oYxCsUserEntity)
        {
            return BaseResult.failed("找不到对应的系统用户");
        }         

        if(oYxcscClientFiledServiceEntity.getState() == 1)
        {
            return BaseResult.failed("该申请的客户端还未完成消息订阅，申请失败");
        }        
        if(oYxcscClientFiledServiceEntity.getState() !=  2 && oYxcscClientFiledServiceEntity.getState() !=  4)
        {
            return BaseResult.failed("该申请处于有效状态，申请失败");
        }
        QueryWrapper<YxCsUserEntity> oYxCsUserEntityQueryWrapper = new QueryWrapper();
        oYxCsUserEntityQueryWrapper.eq("user_id",oCustomerRequestServiceDTO.getUserId());
        List<YxCsUserEntity> oListYxcscMqttServersClient = oUserMapper.selectList(oYxCsUserEntityQueryWrapper);
        YxCsUserEntity oSenderEntity = null;
        if(oListYxcscMqttServersClient.isEmpty())
        {
            return BaseResult.failed("找不到对应的系统用户");
        }        
        oSenderEntity = oListYxcscMqttServersClient.get(0);        
        
        YxcscCustomerAccessServiceEntity oYxcscCustomerAccessServiceEntity = new YxcscCustomerAccessServiceEntity();
        BeanUtil.copyProperties(oYxcscClientFiledServiceEntity, oYxcscCustomerAccessServiceEntity);//channelId,portId,serviceType,sessionId,userId,userName
        oYxcscCustomerAccessServiceEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        
        oYxcscCustomerAccessServiceEntity.setAccessType(1);
        oYxcscCustomerAccessServiceEntity.setAccessTypeName("正常");        
        
        oYxcscCustomerAccessServiceEntity.setCustId(oSenderEntity.getUserId());
        oYxcscCustomerAccessServiceEntity.setCustName(oSenderEntity.getUserName());
        oYxcscCustomerAccessServiceEntity.setStatus(0);
        oYxcscCustomerAccessServiceEntity.setSessionId(oCustomerRequestServiceDTO.getSessionId());
        oYxcscCustomerAccessServiceEntity.setFlockId(oYxcscClientFiledServiceEntity.getFlockId());
        oYxcscCustomerAccessServiceEntity.setCreateTime(System.currentTimeMillis());
        oYxcscCustomerAccessServiceEntity.setUpdateTime(System.currentTimeMillis());
        
        oYxcscClientFiledServiceEntity.setAccessNumber(oYxcscClientFiledServiceEntity.getAccessNumber() + 1);

        UpdateWrapper<YxcscClientFiledServiceEntity> oUpdateWrapper = new UpdateWrapper();
        oUpdateWrapper.eq("session_id", oCustomerRequestServiceDTO.getSessionId());
        oUpdateWrapper.eq("state", 2);
        oUpdateWrapper.set("state", 3);
        oUpdateWrapper.set("access_number", oYxcscClientFiledServiceEntity.getAccessNumber());
        oUpdateWrapper.set("coust_id", oYxCsUserEntity.getUserId().toString());
        oUpdateWrapper.set("coust_name", oYxCsUserEntity.getUserName());
        oUpdateWrapper.set("coust_avatar", oYxCsUserEntity.getPicUrl());
        oUpdateWrapper.set("coust_mobile_phone", oYxCsUserEntity.getMobilePhone());
        oUpdateWrapper.set("current_access_id", oYxcscCustomerAccessServiceEntity.getId());
        //System.out.print("current_access_id:" + oYxcscCustomerAccessServiceEntity.getId());
        int i = oYxcscClientFiledServiceMapper.update(null, oUpdateWrapper);
        if(i == 1)
        {
            oYxcscCustomerAccessServiceMapper.insert(oYxcscCustomerAccessServiceEntity);
            //System.out.print("current_access_id:" + oYxcscCustomerAccessServiceEntity.getId());
            oYxcscClientFiledServiceEntity.setCoustId(oYxCsUserEntity.getUserId());
            oYxcscClientFiledServiceEntity.setCoustName(oYxCsUserEntity.getUserName());
            oYxcscClientFiledServiceEntity.setCoustAvatar(oYxCsUserEntity.getPicUrl());
            oYxcscClientFiledServiceEntity.setCoustMobilePhone(oYxCsUserEntity.getMobilePhone());
            oYxcscClientFiledServiceEntity.setCurrentAccessId(oYxcscCustomerAccessServiceEntity.getId());
            oYxcscClientFiledServiceEntity.setState(3);
            YxcscServiceTalkRecordEntity oYxcscServiceTalkRecordEntity = new YxcscServiceTalkRecordEntity();
            oYxcscServiceTalkRecordEntity.setTalkInfo( "您好，" + oSenderEntity.getUserName() + "已接受了您的服务");
            oYxcscServiceTalkRecordEntity.setTalkInfoType(ContentTypeEnum.MULTITYPE.getCode()); 
            oYxcscServiceTalkRecordEntity.setId(0L);
            oIMqttProcessService.sendTalkMsgByCustomer(oYxcscClientFiledServiceEntity,oYxcscServiceTalkRecordEntity);            
            return BaseResult.success(oYxcscCustomerAccessServiceEntity);
        }
        else
        {
            return BaseResult.failed("该申请已被其他客服处理，申请失败");
        }
    }    
    /*** 
    * 发送聊天消息给某个客户
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult sendTalkInforToClient(SessionTalkRecordDTO oSessionTalkRecordDTO)
    {
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(oSessionTalkRecordDTO.getSessionId());
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        }
        if(oYxcscClientFiledServiceEntity.getState() > 3)
        {
            return BaseResult.failed("该服务已结束不能发送对话信息");
        }
        else if(oYxcscClientFiledServiceEntity.getState()<3)
        {
            return BaseResult.failed("该服务处还没有于不能发送对话信息状态");
        }
        if(!oSessionTalkRecordDTO.getTalkInfoType().toLowerCase().equals("multitype"))
        {
            if(!oSessionTalkRecordDTO.getTalkInfo().toLowerCase().startsWith("http://") && !oSessionTalkRecordDTO.getTalkInfo().toLowerCase().startsWith("https://"))
            {
                return BaseResult.failed("该内容必须是有效的URL");
            }
        }
        oSessionTalkRecordDTO.setTalkInfo(oSessionTalkRecordDTO.getTalkInfo().trim());
        if(oSessionTalkRecordDTO.getSenderId().equals(oYxcscClientFiledServiceEntity.getCoustId()))
        {
            YxcscServiceTalkRecordEntity oYxcscServiceTalkRecordEntity = new YxcscServiceTalkRecordEntity();
            oYxcscServiceTalkRecordEntity.setSenderId(oSessionTalkRecordDTO.getSenderId().toString());
            oYxcscServiceTalkRecordEntity.setSenderName(oYxcscClientFiledServiceEntity.getCoustName());
            oYxcscServiceTalkRecordEntity.setSenderType(2);
            oYxcscServiceTalkRecordEntity.setSessionId(oSessionTalkRecordDTO.getSessionId());
            oYxcscServiceTalkRecordEntity.setAccessServiceId(oYxcscClientFiledServiceEntity.getCurrentAccessId());
            oYxcscServiceTalkRecordEntity.setTalkInfo(oSessionTalkRecordDTO.getTalkInfo());
            oYxcscServiceTalkRecordEntity.setTalkInfoType(oSessionTalkRecordDTO.getTalkInfoType().toLowerCase());
            oYxcscServiceTalkRecordEntity.setCreateTime(System.currentTimeMillis());
            oYxcscServiceTalkRecordMapper.insert(oYxcscServiceTalkRecordEntity);
            oIMqttProcessService.sendTalkMsgByCustomer(oYxcscClientFiledServiceEntity, oYxcscServiceTalkRecordEntity);
            return BaseResult.success(oYxcscServiceTalkRecordEntity.getId());
        }
        else
        {
            return BaseResult.failed("发送人申请人对不上");
        } 
    }    
    /*** 
    * 客服发送聊天消息给某个客服群
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult sendTalkInforToFlock(YxcscFlockTalkRecordDTO oYxcscFlockTalkRecordDTO)
    {
        if(oYxcscFlockTalkRecordDTO.getFlockType() == 0)
        {
            YxCsFlockEntity oYxCsFlockEntity = flockMapper.selectById(oYxcscFlockTalkRecordDTO.getFlockId());
            if(null == oYxCsFlockEntity)
            {
                return BaseResult.failed("选择的群不存在或已被解散");
            }
        }
        
        YxCsUserEntity oYxCsUserEntity = oUserMapper.selectById(oYxcscFlockTalkRecordDTO.getSenderId());
        if(null == oYxCsUserEntity)
        {
            return BaseResult.failed("找不到对应的系统用户");
        }    
        if(null != oYxcscFlockTalkRecordDTO.getCallUserId())
        {
            if(oYxcscFlockTalkRecordDTO.getCallUserId().compareTo(0L)>0)
            {
                YxCsUserEntity oYxCsUserEntityInner = oUserMapper.selectById(oYxcscFlockTalkRecordDTO.getCallUserId());
                if(null == oYxCsUserEntityInner)
                {
                    return BaseResult.failed("在系统中找不到所呼叫用户");
                }                 
            }
        }
        oYxcscFlockTalkRecordDTO.setTalkInfo(oYxcscFlockTalkRecordDTO.getTalkInfo().trim());
        YxcscFlockTalkRecordEntity oYxcscFlockTalkRecordEntity = new YxcscFlockTalkRecordEntity();
        oYxcscFlockTalkRecordEntity.setSenderId(oYxcscFlockTalkRecordDTO.getSenderId());
        oYxcscFlockTalkRecordEntity.setSenderName(oYxCsUserEntity.getUserName());
        oYxcscFlockTalkRecordEntity.setCallUserId(oYxcscFlockTalkRecordDTO.getCallUserId());
        oYxcscFlockTalkRecordEntity.setCreateTime(System.currentTimeMillis());
        oYxcscFlockTalkRecordEntity.setFlockId(oYxcscFlockTalkRecordDTO.getFlockId());
        oYxcscFlockTalkRecordEntity.setTalkInfo(oYxcscFlockTalkRecordDTO.getTalkInfo());
        oYxcscFlockTalkRecordEntity.setTalkInfoType(oYxcscFlockTalkRecordDTO.getTalkInfoType());
        oYxcscFlockTalkRecordEntity.setFlockType(oYxcscFlockTalkRecordDTO.getFlockType());
        oYxcscFlockTalkRecordMapper.insert(oYxcscFlockTalkRecordEntity);        
        oIMqttProcessService.sendFlockTalkMsgByCustomer(oYxcscFlockTalkRecordEntity);
        //oYxcscFlockTalkRecordDTO, oYxcscFlockTalkRecordEntity.getCreateTime(),oYxcscFlockTalkRecordEntity.getId()
        return BaseResult.success(oYxcscFlockTalkRecordEntity.getId());
    }
    /*** 
    * 客服将某个服务指派给别人
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult customerSetServiceToCustomer(Long userId,Long toCustomerId,String sessionId)
    {
        if(Objects.equals(userId, toCustomerId))
        {
            return BaseResult.failed("不能转给自己");
        }
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(sessionId);
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        }        
        YxcscCustomerAccessServiceEntity oCustomerAccessServiceOld = oYxcscCustomerAccessServiceMapper.selectById(oYxcscClientFiledServiceEntity.getCurrentAccessId());
        if(null == oCustomerAccessServiceOld)
        {
            return BaseResult.failed("根据当前处理人找不到处理节点");
        }
        YxCsUserEntity oRecivEntity = oUserMapper.selectById(toCustomerId);
        if(null == oRecivEntity)
        {
            return BaseResult.failed("接收人找不到对应的系统用户");
        }

        YxCsUserEntity oSenderEntity = oUserMapper.selectById(userId);
        if(null == oSenderEntity)
        {
            return BaseResult.failed("发起人找不到对应的系统用户");
        }        

        YxcscCustomerAccessServiceEntity oYxcscCustomerAccessServiceEntity = new YxcscCustomerAccessServiceEntity();
        BeanUtil.copyProperties(oYxcscClientFiledServiceEntity, oYxcscCustomerAccessServiceEntity);//channelId,portId,serviceType,sessionId,userId,userName
        oYxcscCustomerAccessServiceEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        oYxcscCustomerAccessServiceEntity.setAccessType(2);
        oYxcscCustomerAccessServiceEntity.setAccessTypeName("转接到用户");
        oYxcscCustomerAccessServiceEntity.setCustId(toCustomerId);
        oYxcscCustomerAccessServiceEntity.setCustName(oRecivEntity.getUserName());
        oYxcscCustomerAccessServiceEntity.setStatus(0);
        oYxcscCustomerAccessServiceEntity.setSessionId(sessionId);
        //oYxcscCustomerAccessServiceMapper.insert(oYxcscCustomerAccessServiceEntity);
        oCustomerAccessServiceOld.setUpdateTime(System.currentTimeMillis());
        oCustomerAccessServiceOld.setStatus(1);
        //oYxcscCustomerAccessServiceMapper.updateById(oCustomerAccessServiceOld);
        oYxcscClientFiledServiceEntity.setCoustId(toCustomerId);
        oYxcscClientFiledServiceEntity.setCoustName(oRecivEntity.getUserName());
        oYxcscClientFiledServiceEntity.setCoustMobilePhone(oRecivEntity.getMobilePhone());
        oYxcscClientFiledServiceEntity.setCoustAvatar(oRecivEntity.getPicUrl());
        oYxcscClientFiledServiceEntity.setCurrentAccessId(oYxcscCustomerAccessServiceEntity.getId());
        oYxcscClientFiledServiceEntity.setAccessNumber(oYxcscClientFiledServiceEntity.getAccessNumber() + 1);
        oYxcscClientFiledServiceEntity.setUpdateTime(System.currentTimeMillis());
        
        oYxcscCustomerAccessServiceMapper.insert(oYxcscCustomerAccessServiceEntity);
        oYxcscCustomerAccessServiceMapper.updateById(oCustomerAccessServiceOld);
        //workflowMapper.insert(oYxCsWorkflowEntity);
        //oYxcscClientFiledServiceEntity.setWorkflowId(oYxCsWorkflowEntity.getId());
        oYxcscClientFiledServiceMapper.updateById(oYxcscClientFiledServiceEntity);
        oIMqttProcessService.customerSetServiceToCustomer(oSenderEntity, oRecivEntity, oYxcscClientFiledServiceEntity);
        return BaseResult.success();
    }

    /*** 
    * 客服申请技术组协助
    * @param userId
    * @param flockId
    * @param sessionId
    * @return MqttServerInforVO
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult customerSetWorkflowToTechnicalSupportFlock(CustomerSetWorkFlowDTO oCustomerSetWorkFlowDTO)
    {
        //System.out.println("customerSetServiceToTechnicalSupportFlock  进服务了---------------------------------------------------------------------------------------------------------------------------");
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(oCustomerSetWorkFlowDTO.getSessionId());
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        }
       
        YxcscCustomerAccessServiceEntity oCustomerAccessServiceOld = oYxcscCustomerAccessServiceMapper.selectById(oYxcscClientFiledServiceEntity.getCurrentAccessId());
        if(null == oCustomerAccessServiceOld)
        {
            return BaseResult.failed("找不到当前处理人");
        } 
        if(null == oCustomerSetWorkFlowDTO.getFlockId())
        {
            oCustomerSetWorkFlowDTO.setFlockId(Constants.defaultTechFlockId);
        }
        QueryWrapper<TechFlockUserEntity> oTechFlockUserQueryWrapper = new QueryWrapper();
        oTechFlockUserQueryWrapper.eq("flock_id",Constants.defaultTechFlockId);
        List<TechFlockUserEntity> oListTechFlockUser = oTechFlockUserMapper.selectList(oTechFlockUserQueryWrapper);
        if(oListTechFlockUser.isEmpty())
        {
            return BaseResult.failed("当前群没有组员");
        }
        YxCsUserEntity oYxCsUserEntity = oUserMapper.selectById(oCustomerSetWorkFlowDTO.getCustomId());
        if(null == oYxCsUserEntity)
        {
            return BaseResult.failed("在系统中找不到当前用户");
        }
        //工单
        YxCsWorkflowEntity oYxCsWorkflowEntity = new YxCsWorkflowEntity();
        oYxCsWorkflowEntity.setId(null);
        oYxCsWorkflowEntity.setCreateTime(BaseDateTime.getDate());
        oYxCsWorkflowEntity.setSessionId(oCustomerSetWorkFlowDTO.getSessionId());
        oYxCsWorkflowEntity.setCustomId(oYxcscClientFiledServiceEntity.getCoustId());
        oYxCsWorkflowEntity.setCustomName(oYxcscClientFiledServiceEntity.getCoustName());
        oYxCsWorkflowEntity.setClientId(oYxcscClientFiledServiceEntity.getClientId());
        oYxCsWorkflowEntity.setClientName(oYxcscClientFiledServiceEntity.getClientName());
        //oYxCsWorkflowEntity.setReceiveDeptId(oCustomerSetWorkFlowDTO.getFlockId());
        //oYxCsWorkflowEntity.setReceiveDeptName("工单组");
        oYxCsWorkflowEntity.setWorkflowType(0);
        oYxCsWorkflowEntity.setGroupName(oYxCsUserEntity.getMobilePhone());
        oYxCsWorkflowEntity.setEndFlag(0);
        oYxCsWorkflowEntity.setDescription(oCustomerSetWorkFlowDTO.getDescription());
        oYxCsWorkflowEntity.setChannelCode(oYxcscClientFiledServiceEntity.getChannelCode());
        oYxCsWorkflowEntity.setChannelName(oYxcscClientFiledServiceEntity.getChannelName());
        oYxCsWorkflowEntity.setPortCode(oYxcscClientFiledServiceEntity.getPortCode());
        oYxCsWorkflowEntity.setPortName(oYxcscClientFiledServiceEntity.getPortName());
        workflowMapper.insert(oYxCsWorkflowEntity);
        //原来的处理人修改
        //oCustomerAccessServiceOld.setUpdateTime(System.currentTimeMillis());
        //oCustomerAccessServiceOld.setStatus(1);
        //服务申请基础信息修改    
        UpdateWrapper<YxcscClientFiledServiceEntity> oUpdateWrapper = new UpdateWrapper();
        oUpdateWrapper.eq("session_id", oCustomerSetWorkFlowDTO.getSessionId());
        //发送群聊天记录         
        YxcscFlockTalkRecordEntity oYxcscFlockTalkRecordEntity = new YxcscFlockTalkRecordEntity();
        oYxcscFlockTalkRecordEntity.setSenderId(0L);
        oYxcscFlockTalkRecordEntity.setSenderName("system");
        oYxcscFlockTalkRecordEntity.setCreateTime(System.currentTimeMillis());
        oYxcscFlockTalkRecordEntity.setFlockId(oCustomerSetWorkFlowDTO.getFlockId());
        oYxcscFlockTalkRecordEntity.setFlockType(1);
        JSONObject oJSONObject = (JSONObject)JSON.toJSON(oYxCsWorkflowEntity);
        oYxcscFlockTalkRecordEntity.setTalkInfo(oJSONObject.toString());
        oYxcscFlockTalkRecordEntity.setTalkInfoType(ContentTypeEnum.WORKFLOW.getCode());
        oYxcscFlockTalkRecordMapper.insert(oYxcscFlockTalkRecordEntity);  
        
        oYxcscClientFiledServiceEntity.setWorkflowId(oYxCsWorkflowEntity.getId());
        oUpdateWrapper.set("workflow_id",oYxCsWorkflowEntity.getId());
        oYxcscClientFiledServiceMapper.update(null,oUpdateWrapper);
        oIMqttProcessService.customerSetWorkflowToTechnicalSupportFlock(oYxcscClientFiledServiceEntity, oYxCsWorkflowEntity,oYxcscFlockTalkRecordEntity, oListTechFlockUser);
        return BaseResult.success();
    } 
    /*** 
    * 技术支持组成员申请接受工单/技术支持
    * @param userId
    * @param flockId
    * @param sessionId
    * @return BaseResult
    **/
    @Override
    public BaseResult technicalSupportAcceptWorkFlow(Long userId,Long workflowId)
    {
        QueryWrapper<TechFlockUserEntity> oTechFlockUserQueryWrapper = new QueryWrapper();
        oTechFlockUserQueryWrapper.eq("user_id",userId);
        List<TechFlockUserEntity> oListTechFlockUser = oTechFlockUserMapper.selectList(oTechFlockUserQueryWrapper);
        if(oListTechFlockUser.isEmpty())
        {
            return BaseResult.failed("技术组没有该组员");
        }        
        TechFlockUserEntity oTechFlockUserEntity = oListTechFlockUser.get(0);
        YxCsWorkflowEntity oYxCsWorkflowEntity = workflowMapper.selectById(workflowId);
        if(null == oYxCsWorkflowEntity)
        {
            return BaseResult.failed("找不到当前工单");
        }
           
        if(oYxCsWorkflowEntity.getEndFlag() != 0 )
        {
            return BaseResult.failed("当前工单已有人:" +  oYxCsWorkflowEntity.getHandelName() + "处理，申请失败");
        }
        oYxCsWorkflowEntity.setHandelTime(BaseDateTime.getDate());
        UpdateWrapper<YxCsWorkflowEntity> oUpdateWrapper = new UpdateWrapper();
        oUpdateWrapper.eq("workflow_id", workflowId);
        oUpdateWrapper.eq("end_flag", 0);
        oUpdateWrapper.set("end_flag", 1);
        oUpdateWrapper.set("workflow_type", 0);
        oUpdateWrapper.set("handel_id", userId);
        oUpdateWrapper.set("handel_name", oTechFlockUserEntity.getUserName());
        oUpdateWrapper.set("receive_dept_id", oTechFlockUserEntity.getDeptId());
        oUpdateWrapper.set("receive_dept_name", oTechFlockUserEntity.getDeptName());
        oUpdateWrapper.set("handel_time", oYxCsWorkflowEntity.getHandelTime());
        oYxCsWorkflowEntity.setEndFlag(1);
        oYxCsWorkflowEntity.setWorkflowType(1);
        oYxCsWorkflowEntity.setHandelId(userId);
        oYxCsWorkflowEntity.setHandelName(oTechFlockUserEntity.getUserName());
        oYxCsWorkflowEntity.setReceiveDeptId(Long.valueOf(oTechFlockUserEntity.getDeptId()));
        oYxCsWorkflowEntity.setReceiveDeptName(oTechFlockUserEntity.getDeptName());
        workflowMapper.update(null, oUpdateWrapper);
        oIMqttProcessService.technicalSupportAcceptWorkFlow(oYxCsWorkflowEntity);
        return BaseResult.success("申请成功");
        
    }
    /*** 
    * 技术支持组管理员指派工单/技术支持
    * @param managerId
    * @param userId
    * @param workflowId
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult technicalSupportManagerSetWorkFlow(Long managerId,Long userId,Long workflowId)
    {
        YxCsUserEntity oYxCsUserEntity = oUserMapper.selectById(managerId);
        if(null == oYxCsUserEntity)
        {
            return BaseResult.failed("根据管理员Id找不到对应的系统用户");
        }
        QueryWrapper<TechFlockUserEntity> oTechFlockUserQueryWrapper = new QueryWrapper();
        oTechFlockUserQueryWrapper.eq("user_id",userId);
        List<TechFlockUserEntity> oListTechFlockUser = oTechFlockUserMapper.selectList(oTechFlockUserQueryWrapper);
        if(oListTechFlockUser.isEmpty())
        {
            return BaseResult.failed("技术组没有该组员");
        }
        TechFlockUserEntity oTechFlockUserEntity = oListTechFlockUser.get(0);
        YxCsWorkflowEntity oYxCsWorkflowEntity = workflowMapper.selectById(workflowId);
        if(null == oYxCsWorkflowEntity)
        {
            return BaseResult.failed("找不到当前工单");
        } 
        if(oYxCsWorkflowEntity.getEndFlag() != 0)
        {
            return BaseResult.failed("当前工单已有处理人:" +  oYxCsWorkflowEntity.getHandelName() + "，指派失败");
        }
        //当前工单还没有处理人
        if(oYxCsWorkflowEntity.getEndFlag() == 0)
        {
            oYxCsWorkflowEntity.setHandelTime(BaseDateTime.getDate());
            UpdateWrapper<YxCsWorkflowEntity> oUpdateWrapper = new UpdateWrapper();
            oUpdateWrapper.eq("workflow_id", workflowId);
            oUpdateWrapper.eq("end_flag", 0);
            oUpdateWrapper.set("end_flag", 1);
            oUpdateWrapper.set("workflow_type", 0);
            oUpdateWrapper.set("handel_id", userId);
            oUpdateWrapper.set("handel_name", oTechFlockUserEntity.getUserName());
            oUpdateWrapper.set("receive_dept_id", oTechFlockUserEntity.getDeptId());
            oUpdateWrapper.set("receive_dept_name", oTechFlockUserEntity.getDeptName());     
            oUpdateWrapper.set("handel_time", oYxCsWorkflowEntity.getHandelTime());
            oUpdateWrapper.set("channel_code", oYxCsWorkflowEntity.getChannelCode());
            oUpdateWrapper.set("channel_name", oYxCsWorkflowEntity.getChannelName());
            oUpdateWrapper.set("port_code", oYxCsWorkflowEntity.getPortCode());
            oUpdateWrapper.set("port_name", oYxCsWorkflowEntity.getPortName());
            oYxCsWorkflowEntity.setEndFlag(1);
            oYxCsWorkflowEntity.setWorkflowType(1);
            oYxCsWorkflowEntity.setHandelId(userId);
            oYxCsWorkflowEntity.setHandelName(oTechFlockUserEntity.getUserName());
            oYxCsWorkflowEntity.setReceiveDeptId(Long.valueOf(oTechFlockUserEntity.getDeptId()));
            oYxCsWorkflowEntity.setReceiveDeptName(oTechFlockUserEntity.getDeptName());            
            workflowMapper.update(null, oUpdateWrapper);
            oIMqttProcessService.technicalSupportAcceptWorkFlow(oYxCsWorkflowEntity);
            return BaseResult.success("指派成功");
        }
        return BaseResult.success();
    }
    /*** 
    * 技术支持组成员完成技术支持
    * @param oTechnicalSupportFinishWorkFlowDTO
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult technicalSupportFinishWorkFlow(TechnicalSupportFinishWorkFlowDTO oTechnicalSupportFinishWorkFlowDTO)
    {

        if(oTechnicalSupportFinishWorkFlowDTO.getHandelResult() != 0 && oTechnicalSupportFinishWorkFlowDTO.getHandelResult() != 1)
        {
            BaseResult.failed("工单的处理结果只能为0和1，0表示处理失败，1表示处理成功");
        } 
        YxCsWorkflowEntity oYxCsWorkflowEntity = workflowMapper.selectById(oTechnicalSupportFinishWorkFlowDTO.getWorkflowId());
        if(null == oYxCsWorkflowEntity)
        {
            return BaseResult.failed("找不到当前工单");
        }         
        if(!oYxCsWorkflowEntity.getHandelId().equals(oTechnicalSupportFinishWorkFlowDTO.getUserId()))
        {
            return BaseResult.failed("当前工单的处理人为" + oYxCsWorkflowEntity.getHandelName() + ",你没有处理权限");
        }
        if(oYxCsWorkflowEntity.getEndFlag() != 1)
        {
            return BaseResult.failed("当前工单处于待领取状态，处理失败");
        } 
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(oYxCsWorkflowEntity.getSessionId());
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        }        
        QueryWrapper<TechFlockUserEntity> oTechFlockUserQueryWrapper = new QueryWrapper();
        oTechFlockUserQueryWrapper.eq("flock_id",Constants.defaultTechFlockId);
        List<TechFlockUserEntity> oListTechFlockUser = oTechFlockUserMapper.selectList(oTechFlockUserQueryWrapper);
        if(oListTechFlockUser.isEmpty())
        {
            return BaseResult.failed("当前群没有组员");
        }
        
        UpdateWrapper<YxCsWorkflowEntity> oUpdateWrapper = new UpdateWrapper();
        oUpdateWrapper.eq("workflow_id", oTechnicalSupportFinishWorkFlowDTO.getWorkflowId());
        //oUpdateWrapper.eq("end_flag", 0);
        oUpdateWrapper.set("end_flag", 2);
        oUpdateWrapper.set("handel_result", oTechnicalSupportFinishWorkFlowDTO.getHandelResult());
        oUpdateWrapper.set("handel_result_description", oTechnicalSupportFinishWorkFlowDTO.getHandelResultDescription());
        oUpdateWrapper.set("handel_time", BaseDateTime.getDate());
        //当工单为处理失败时，将产生新工单，有其他人来处理
        if(oTechnicalSupportFinishWorkFlowDTO.getHandelResult() == 0)
        {
            //新增工单
            YxCsWorkflowEntity oYxCsWorkflowEntityNew = new YxCsWorkflowEntity();
            oYxCsWorkflowEntityNew.setId(null);
            oYxCsWorkflowEntityNew.setCreateTime(BaseDateTime.getDate());
            oYxCsWorkflowEntityNew.setSessionId(oYxCsWorkflowEntity.getSessionId());
            oYxCsWorkflowEntityNew.setCustomId(oYxCsWorkflowEntity.getCustomId());
            oYxCsWorkflowEntityNew.setCustomName(oYxCsWorkflowEntity.getCustomName());
            oYxCsWorkflowEntityNew.setClientId(oYxCsWorkflowEntity.getClientId());
            oYxCsWorkflowEntityNew.setClientName(oYxCsWorkflowEntity.getClientName());
            //oYxCsWorkflowEntityNew.setGroupName(oTechFlockUserEntity.getMobilePhone());
            //oYxCsWorkflowEntityNew.setReceiveDeptId(Long.valueOf(oTechFlockUserEntity.getDeptId()));
            //oYxCsWorkflowEntityNew.setReceiveDeptName(oTechFlockUserEntity.getDeptName());
            oYxCsWorkflowEntityNew.setWorkflowType(0);
            oYxCsWorkflowEntityNew.setEndFlag(0);
            oYxCsWorkflowEntityNew.setDescription(oYxCsWorkflowEntity.getDescription());
            //更新会话/服务
            UpdateWrapper<YxcscClientFiledServiceEntity> oUpdateWrapperClientFiledService = new UpdateWrapper();
            oUpdateWrapperClientFiledService.eq("session_id", oYxCsWorkflowEntityNew.getSessionId());            
            //新增聊天记录              
            YxcscFlockTalkRecordEntity oYxcscFlockTalkRecordEntity = new YxcscFlockTalkRecordEntity();
            oYxcscFlockTalkRecordEntity.setSenderId(0L);
            oYxcscFlockTalkRecordEntity.setSenderName("system");
            oYxcscFlockTalkRecordEntity.setCreateTime(System.currentTimeMillis());
            oYxcscFlockTalkRecordEntity.setFlockId(Constants.defaultTechFlockId);
            oYxcscFlockTalkRecordEntity.setFlockType(1);

            workflowMapper.insert(oYxCsWorkflowEntityNew);
            JSONObject oJSONObject = (JSONObject)JSON.toJSON(oYxCsWorkflowEntityNew);
            oYxcscFlockTalkRecordEntity.setTalkInfo(oJSONObject.toString());
            oYxcscFlockTalkRecordEntity.setTalkInfoType(ContentTypeEnum.WORKFLOW.getCode());            
            oYxcscFlockTalkRecordMapper.insert(oYxcscFlockTalkRecordEntity);
            oUpdateWrapperClientFiledService.set("workflow_id",oYxCsWorkflowEntityNew.getId());
            oYxcscClientFiledServiceMapper.update(null,oUpdateWrapperClientFiledService); 
            oIMqttProcessService.customerSetWorkflowToTechnicalSupportFlock(oYxcscClientFiledServiceEntity,oYxCsWorkflowEntityNew,oYxcscFlockTalkRecordEntity,oListTechFlockUser);
            //oIMqttProcessService.technicalSupportFinishWorkFlow(oYxCsWorkflowEntityNew,oYxcscFlockTalkRecordEntity);
        }
        else
        {
            oIMqttProcessService.technicalSupportAcceptWorkFlow(oYxCsWorkflowEntity);
        }
        workflowMapper.update(null, oUpdateWrapper);
        
        return BaseResult.success("处理成功");
    }
    public BaseResult customerSetServiceToFlockOld(Long userId,Long flockId,String sessionId)
    {
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(sessionId);
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        }
        YxcscCustomerAccessServiceEntity oCustomerAccessServiceOld = oYxcscCustomerAccessServiceMapper.selectById(oYxcscClientFiledServiceEntity.getCurrentAccessId());
        if(null == oCustomerAccessServiceOld)
        {
            return BaseResult.failed("找不到当前处理人");
        }    
        YxCsFlockEntity oYxCsFlockEntity = flockMapper.selectById(flockId);
        if(null == oYxCsFlockEntity)
        {
            return BaseResult.failed("选择的群不存在或已被解散");
        }        
        QueryWrapper<YxCsFlockUserEntity> oYxCsFlockUserQueryWrapper = new QueryWrapper();
        oYxCsFlockUserQueryWrapper.eq("flock_id",flockId).orderByAsc("flock_id");
        List<YxCsFlockUserEntity> oListYxCsFlockUserEntity = oYxCsFlockUserMapper.selectList(oYxCsFlockUserQueryWrapper);
        if(oListYxCsFlockUserEntity.isEmpty())
        {
            return BaseResult.failed("当前群没有组员");
        }

        //服务流转到群
        YxCsWorkflowEntity oYxCsWorkflowEntity = new YxCsWorkflowEntity();
        oYxCsWorkflowEntity.setId(null);
        oYxCsWorkflowEntity.setCreateTime(BaseDateTime.getDate());
        oYxCsWorkflowEntity.setSessionId(sessionId);
        oYxCsWorkflowEntity.setCustomId(oYxcscClientFiledServiceEntity.getCoustId());
        oYxCsWorkflowEntity.setCustomName(oYxcscClientFiledServiceEntity.getCoustName());
        oYxCsWorkflowEntity.setClientId(oYxcscClientFiledServiceEntity.getClientId());
        oYxCsWorkflowEntity.setClientName(oYxcscClientFiledServiceEntity.getClientName());
        oYxCsWorkflowEntity.setReceiveDeptId(flockId);
        oYxCsWorkflowEntity.setReceiveDeptName(oYxCsFlockEntity.getFlockName());
        oYxCsWorkflowEntity.setWorkflowType(3);
        oYxCsWorkflowEntity.setCreateTime(BaseDateTime.getDate());
        //原来的处理人修改
        oCustomerAccessServiceOld.setUpdateTime(System.currentTimeMillis());
        oCustomerAccessServiceOld.setStatus(1);
        //服务申请基础信息修改
        oYxcscClientFiledServiceEntity.setState(4);
        oYxcscClientFiledServiceEntity.setUpdateTime(oCustomerAccessServiceOld.getUpdateTime());
        oYxcscClientFiledServiceEntity.setCoustId(null);
        oYxcscClientFiledServiceEntity.setCoustName(null);        
        UpdateWrapper<YxcscClientFiledServiceEntity> oUpdateWrapper = new UpdateWrapper();
        oUpdateWrapper.eq("session_id", sessionId);
        oUpdateWrapper.set("state", 4);
        oUpdateWrapper.set("coust_id",null);
        oUpdateWrapper.set("coust_name",null);
        oUpdateWrapper.set("coust_avatar",null);        
        oYxcscCustomerAccessServiceMapper.updateById(oCustomerAccessServiceOld);
        workflowMapper.insert(oYxCsWorkflowEntity);
        oYxcscClientFiledServiceEntity.setWorkflowId(oYxCsWorkflowEntity.getId());
        oUpdateWrapper.set("workflow_id",oYxCsWorkflowEntity.getId());
        oYxcscClientFiledServiceMapper.update(null,oUpdateWrapper);
        //oIMqttProcessService.customerSetServiceToFlock(oYxcscClientFiledServiceEntity, userId, oListYxCsFlockUserEntity);
        return BaseResult.success();
    }     
    
    /*** 
    * 客服完成某个服务接口
    * @param userId
    * @return MqttServerInforVO
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult customerFinishOneService(Long userId,String sessionId)
    {
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(sessionId);
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        } 
        if(!oYxcscClientFiledServiceEntity.getCoustId().equals(userId))
        {
            return BaseResult.failed("没有操作权限");
        }
        YxcscCustomerAccessServiceEntity oCustomerAccessServiceOld = oYxcscCustomerAccessServiceMapper.selectById(oYxcscClientFiledServiceEntity.getCurrentAccessId());
        if(null == oCustomerAccessServiceOld)
        {
            return BaseResult.failed("找不到当前处理人");
        }
        YxCsWorkflowEntity oYxCsWorkflowEntity = null;
        if(oCustomerAccessServiceOld.getAccessType() != 1)
        {
            oYxCsWorkflowEntity = workflowMapper.selectById(oYxcscClientFiledServiceEntity.getWorkflowId());
        }
        oYxcscClientFiledServiceEntity.setState(5);
        oYxcscClientFiledServiceEntity.setUpdateTime(System.currentTimeMillis());
        oCustomerAccessServiceOld.setUpdateTime(oYxcscClientFiledServiceEntity.getUpdateTime());
        oCustomerAccessServiceOld.setStatus(1);
        oYxcscClientFiledServiceMapper.updateById(oYxcscClientFiledServiceEntity);
        oYxcscCustomerAccessServiceMapper.updateById(oCustomerAccessServiceOld);
        if(null != oYxCsWorkflowEntity)
        {
            oYxCsWorkflowEntity.setEndFlag(1);
            oYxCsWorkflowEntity.setHandelTime(BaseDateTime.getDate());
            workflowMapper.updateById(oYxCsWorkflowEntity);
        }
        return BaseResult.success();
    }
    /*** 
    * 客服邀请客服评分接口
    * @param sessionId  服务Id
    * @return Page
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult customerInviteClientScore(Long userId,String sessionId)
    {
        YxcscClientFiledServiceEntity oYxcscClientFiledServiceEntity = oYxcscClientFiledServiceMapper.selectById(sessionId);
        if(null == oYxcscClientFiledServiceEntity)
        {
            return BaseResult.failed("找不到对应的服务申请");
        } 
        if(!oYxcscClientFiledServiceEntity.getCoustId().equals(userId))
        {
            return BaseResult.failed("没有操作权限");
        }
        YxCsUserEntity oYxCsUserEntity = oUserMapper.selectById(userId);
        if(null == oYxCsUserEntity)
        {
            return BaseResult.failed("找不到对应的系统用户");
        }
        oIMqttProcessService.customerInviteClientScore(oYxCsUserEntity, oYxcscClientFiledServiceEntity);
        return BaseResult.success();
    }
    /*** 
    * 客服查询服务聊天接口
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @param oYxcscFlockTalkRecordDTO
    * @return Page
    **/
    @Override
    public BaseResult customerQueryTalkRecord(int currentPage,int pageSize,YxcscServiceTalkRecordDTO oYxcscServiceTalkRecordDTO)
    {
        if(pageSize > 50)
        {
            return BaseResult.failed("分页大小不能超过50条记录");
        }
        Page<YxcscServiceTalkRecordEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxcscServiceTalkRecordEntity> oYxcscServiceTalkRecordQueryWrapper = new QueryWrapper();
        oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getId()),"record_id",oYxcscServiceTalkRecordDTO.getId());
        oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getSessionId()),"session_id",oYxcscServiceTalkRecordDTO.getSessionId());
        oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getSenderId()),"sender_id",oYxcscServiceTalkRecordDTO.getSenderId());
        oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getSenderName()),"sender_name",oYxcscServiceTalkRecordDTO.getSenderName());
        oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getSenderType()),"sender_type",oYxcscServiceTalkRecordDTO.getSenderType());
        oYxcscServiceTalkRecordQueryWrapper.like(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getTalkInfo()),"talk_info","%"+oYxcscServiceTalkRecordDTO.getTalkInfo()+"%");
        oYxcscServiceTalkRecordQueryWrapper.orderByDesc("create_time");
        //oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getCreateTime()),"create_time",oYxcscServiceTalkRecordDTO.getCreateTime());
        //oYxcscServiceTalkRecordQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscServiceTalkRecordDTO.getSendFlag()),"send_flag",oYxcscServiceTalkRecordDTO.getSendFlag());
        return BaseResult.success(oYxcscServiceTalkRecordMapper.selectPage(pageParams, oYxcscServiceTalkRecordQueryWrapper));
    }
    /*** 
    * 客服查询群聊天记录接口
    * @param flockId 群Id
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @return Page
    **/
    @Override
    public BaseResult customerQueryFlockTalkRecord(Long flockId,Integer currentPage,Integer pageSize)
    {
        if(pageSize > 50)
        {
            return BaseResult.failed("分页大小不能超过50条记录");
        }
        Page<YxcscFlockTalkRecordEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxcscFlockTalkRecordEntity> oYxcscFlockTalkRecordEntityQueryWrapper = new QueryWrapper();
        oYxcscFlockTalkRecordEntityQueryWrapper.eq("flock_id",flockId);
        oYxcscFlockTalkRecordEntityQueryWrapper.orderByDesc("create_time");
        return BaseResult.success(oYxcscFlockTalkRecordMapper.selectPage(pageParams, oYxcscFlockTalkRecordEntityQueryWrapper));
    }
    /*** 
    * 客服查询服务接口
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @param oCustomQueryServiceDTO
    * @return Page
    **/
    @Override
    public BaseResult customerQueryService(int currentPage,int pageSize,CustomQueryServiceDTO oYxcscClientFiledServiceEntity)
    {
        if(null != oYxcscClientFiledServiceEntity.getStartTime() && null != oYxcscClientFiledServiceEntity.getEndTime())
        {
            if(oYxcscClientFiledServiceEntity.getStartTime()>=oYxcscClientFiledServiceEntity.getEndTime())
            {
                return BaseResult.failed("开始时间只能小于结束时间");
            }
        }
        if(pageSize > 50)
        {
            return BaseResult.failed("分页大小不能超过50条记录");
        }
        Page<YxcscClientFiledServiceEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxcscClientFiledServiceEntity> oYxcscClientFiledServiceQueryWrapper = new QueryWrapper();
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getId()),"session_id",oYxcscClientFiledServiceEntity.getId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getCoustId()),"coust_id",oYxcscClientFiledServiceEntity.getCoustId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getCoustName()),"coust_name",oYxcscClientFiledServiceEntity.getCoustName());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getCoustAvatar()),"coust_avatar",oYxcscClientFiledServiceEntity.getCoustAvatar());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getChannelId()),"channel_id",oYxcscClientFiledServiceEntity.getChannelId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getChannelCode()),"channel_code",oYxcscClientFiledServiceEntity.getChannelCode());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getPortId()),"port_id",oYxcscClientFiledServiceEntity.getPortId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getPortCode()),"port_code",oYxcscClientFiledServiceEntity.getPortCode());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getServiceType()),"service_type",oYxcscClientFiledServiceEntity.getServiceType());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getOrderId()),"order_id",oYxcscClientFiledServiceEntity.getOrderId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getOrderState()),"order_state",oYxcscClientFiledServiceEntity.getOrderState());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getProductId()),"product_id",oYxcscClientFiledServiceEntity.getProductId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getProductType()),"product_type",oYxcscClientFiledServiceEntity.getProductType());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getEntryId()),"entry_id",oYxcscClientFiledServiceEntity.getEntryId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getEntryCode()),"entry_code",oYxcscClientFiledServiceEntity.getEntryCode());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getSolveState()),"solve_state",oYxcscClientFiledServiceEntity.getSolveState());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getSolveStateName()),"solve_state_name",oYxcscClientFiledServiceEntity.getSolveStateName());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getClientId()),"client_id",oYxcscClientFiledServiceEntity.getClientId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getClientName()),"client_name",oYxcscClientFiledServiceEntity.getClientName());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getClientAvatar()),"client_avatar",oYxcscClientFiledServiceEntity.getClientAvatar());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getCurrentAccessId()),"current_access_id",oYxcscClientFiledServiceEntity.getCurrentAccessId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getAccessNumber()),"access_number",oYxcscClientFiledServiceEntity.getAccessNumber());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getState()),"state",oYxcscClientFiledServiceEntity.getState());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getEvaluate()),"evaluate",oYxcscClientFiledServiceEntity.getEvaluate());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getEvaluateDetail()),"evaluate_detail",oYxcscClientFiledServiceEntity.getEvaluateDetail());
        //oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getUpdateTime()),"update_time",oYxcscClientFiledServiceEntity.getUpdateTime());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getShowMessage()),"show_message",oYxcscClientFiledServiceEntity.getShowMessage());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getShopId()),"shop_id",oYxcscClientFiledServiceEntity.getShopId());
        oYxcscClientFiledServiceQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxcscClientFiledServiceEntity.getWorkflowId()),"workflow_id",oYxcscClientFiledServiceEntity.getWorkflowId());
        if(null != oYxcscClientFiledServiceEntity.getStartTime() && null != oYxcscClientFiledServiceEntity.getEndTime())
        {
            oYxcscClientFiledServiceQueryWrapper.between("create_time",oYxcscClientFiledServiceEntity.getStartTime(),oYxcscClientFiledServiceEntity.getEndTime());
        }
        oYxcscClientFiledServiceQueryWrapper.orderByDesc("create_time");
        return BaseResult.success(oYxcscClientFiledServiceMapper.selectPage(pageParams, oYxcscClientFiledServiceQueryWrapper));        
    }
    /*** 
    * 客服查询服务详情接口
    * @param sessionId  服务Id
    * @return Page
    **/
    @Override
    public BaseResult customerQueryServiceById(String sessionId)
    {
        return BaseResult.success(oYxcscClientFiledServiceMapper.selectById(sessionId));        
    }    
    /*** 
    * 客服查询服务流转接口
    * @param currentPage 当前页
    * @param pageSize 分页大小
    * @param oCustomQueryServiceDTO
    * @return Page
    **/
    @Override
    public BaseResult customQueryWorkflow(int currentPage,int pageSize,CustomQueryWorkflowDTO oYxCsWorkflowEntity)
    {
        if(pageSize > 50)
        {
            return BaseResult.failed("分页大小不能超过50条记录");
        }       
        Page<YxCsWorkflowEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxCsWorkflowEntity> oYxCsWorkflowQueryWrapper = new QueryWrapper();
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getId()),"workflow_id",oYxCsWorkflowEntity.getId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getGroupId()),"group_id",oYxCsWorkflowEntity.getGroupId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getGroupName()),"group_name",oYxCsWorkflowEntity.getGroupName());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getCustomId()),"custom_id",oYxCsWorkflowEntity.getCustomId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getCustomName()),"custom_name",oYxCsWorkflowEntity.getCustomName());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getWorkflowType()),"workflow_type",oYxCsWorkflowEntity.getWorkflowType());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getWorkflowTypeName()),"workflow_type_name",oYxCsWorkflowEntity.getWorkflowTypeName());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getReceiveDeptId()),"receive_dept_id",oYxCsWorkflowEntity.getReceiveDeptId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getReceiveDeptName()),"receive_dept_name",oYxCsWorkflowEntity.getReceiveDeptName());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getHandelId()),"handel_id",oYxCsWorkflowEntity.getHandelId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getHandelName()),"handel_name",oYxCsWorkflowEntity.getHandelName());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getProblemId()),"problem_id",oYxCsWorkflowEntity.getProblemId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getHandelProId()),"handel_pro_id",oYxCsWorkflowEntity.getHandelProId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getCreateTime()),"create_time",oYxCsWorkflowEntity.getCreateTime());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getHandelTime()),"handel_time",oYxCsWorkflowEntity.getHandelTime());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getDescription()),"description",oYxCsWorkflowEntity.getDescription());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getEndFlag()),"end_flag",oYxCsWorkflowEntity.getEndFlag());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getSessionId()),"session_id",oYxCsWorkflowEntity.getSessionId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getClientId()),"client_id",oYxCsWorkflowEntity.getClientId());
        oYxCsWorkflowQueryWrapper.eq(ObjectUtil.isNotEmpty(oYxCsWorkflowEntity.getClientName()),"client_name",oYxCsWorkflowEntity.getClientName());
        return BaseResult.success(workflowMapper.selectPage(pageParams, oYxCsWorkflowQueryWrapper));        
    }
    /*** 
    * 客服查询服务流转详情接口
    * @param workflowId  服务流转记录Id
    * @return Page
    **/
    @Override
    public BaseResult customQueryWorkflowById(Long workflowId)
    {  
        return BaseResult.success(workflowMapper.selectById(workflowId));
    } 
    /*** 
    * 客服查询技术支持组成员列表接口
    * @return Page
    **/
    @Override
    public BaseResult customerQueryTechnicalSupportFlockUsers()
    {
        QueryWrapper<TechFlockUserEntity> oTechFlockUserQueryWrapper = new QueryWrapper();
        oTechFlockUserQueryWrapper.eq("flock_id",Constants.defaultTechFlockId);
        List<TechFlockUserEntity> oListTechFlockUser = oTechFlockUserMapper.selectList(oTechFlockUserQueryWrapper);
        if(oListTechFlockUser.isEmpty())
        {
            return BaseResult.failed("当前群没有组员");
        }
        return BaseResult.success(oListTechFlockUser);        
    }
    /*** 
    *客服撤回/删除聊天记录接口
    * @param customerId 客服Id
    * @param recordId 聊天记录Id
    * @param operationState 操作状态:0正常的消息,1删除的消息,2撤回的消息
    * @return BaseResult
    **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult customerRevokeServiceTalkRecord(Long customerId,Long recordId,Integer operationState)
    {
        if(operationState != 1  && operationState != 2)
        {
            return BaseResult.failed("operationState只能等于1[删除]或2[撤回]");
        }
        YxcscServiceTalkRecordEntity oYxcscServiceTalkRecordEntity = oYxcscServiceTalkRecordMapper.selectById(recordId);
        if(null == oYxcscServiceTalkRecordEntity)
        {
            return BaseResult.failed("找不到聊天记录");
        }
        if((System.currentTimeMillis() - oYxcscServiceTalkRecordEntity.getCreateTime()) > 120000)
        {
            return BaseResult.failed("聊天记录创建时间超过当前时间两分钟，不能撤回/删除");
        }
        if(!oYxcscServiceTalkRecordEntity.getSenderId().equals(customerId.toString()))
        {
            return BaseResult.failed("没有撤回/删除聊天记录权限，不能撤回");
        }
        oYxcscServiceTalkRecordEntity.setOperationState(operationState);
        oYxcscServiceTalkRecordEntity.setOperationTime(System.currentTimeMillis());
        oYxcscServiceTalkRecordMapper.updateById(oYxcscServiceTalkRecordEntity);
        //if(operationState == 2)
        //{
            oIMqttProcessService.customerRevokeServiceTalkRecord(recordId, oYxcscServiceTalkRecordEntity.getSessionId());
        //}
        return BaseResult.success();
    }
    /*** 
    * 系统用户撤回/删除群聊天记录接口
    * @param userId 系统用户Id
    * @param recordId 聊天记录Id
    * @param operationState 操作状态:0正常的消息,1删除的消息,2撤回的消息
    * @return BaseResult
    **/
    @Override
    public BaseResult userRevokeFlockTalkRecord(Long userId,Long recordId,Integer operationState)
    {
        if(operationState != 1  && operationState != 2)
        {
            return BaseResult.failed("operationState只能等于1[删除]或2[撤回]");
        }
        YxcscFlockTalkRecordEntity oYxcscFlockTalkRecordEntity = oYxcscFlockTalkRecordMapper.selectById(recordId);
        if(null == oYxcscFlockTalkRecordEntity)
        {
            return BaseResult.failed("找不到聊天记录");
        }
        if(!Objects.equals(oYxcscFlockTalkRecordEntity.getSenderId(), userId))
        {
            return BaseResult.failed("没有撤回/删除聊天记录权限，不能撤回");
        }
        if((System.currentTimeMillis() - oYxcscFlockTalkRecordEntity.getCreateTime()) > 120000)
        {
            return BaseResult.failed("聊天记录创建时间超过当前时间两分钟，不能撤回/删除");
        }        
        oYxcscFlockTalkRecordEntity.setOperationState(operationState);
        oYxcscFlockTalkRecordEntity.setOperationTime(System.currentTimeMillis());
        oYxcscFlockTalkRecordMapper.updateById(oYxcscFlockTalkRecordEntity);
        //if(operationState == 2)
        //{
            List<MemberVo> list = new ArrayList();
            if(oYxcscFlockTalkRecordEntity.getFlockType() == 0)
            {
                QueryWrapper<CustomerFlockUserEntity> oCustomerFlockQueryWrapper = new QueryWrapper();
                oCustomerFlockQueryWrapper.eq("flock_id",oYxcscFlockTalkRecordEntity.getFlockId());
                List<CustomerFlockUserEntity> listCustomersInFlock = oCustomerFlockUserMapper.selectList(oCustomerFlockQueryWrapper);
                if(listCustomersInFlock.isEmpty())
                {
                    return BaseResult.success("操作成功，但群成员被清空，没法通知其他成员");
                }
                for(CustomerFlockUserEntity oCustomerFlockUserEntityInner : listCustomersInFlock)
                {
                    MemberVo oMemberVo = new MemberVo();
                    oMemberVo.setId(oCustomerFlockUserEntityInner.getUserId());
                    oMemberVo.setUserName(oCustomerFlockUserEntityInner.getUserName());
                    oMemberVo.setPhone(oCustomerFlockUserEntityInner.getMobilePhone());
                    list.add(oMemberVo);
                }            
            }
            else
            {
                QueryWrapper<TechFlockUserEntity> oTechFlockUserQueryWrapper = new QueryWrapper();
                oTechFlockUserQueryWrapper.eq("flock_id",oYxcscFlockTalkRecordEntity.getFlockId());
                List<TechFlockUserEntity> oListTechFlockUsers = oTechFlockUserMapper.selectList(oTechFlockUserQueryWrapper);
                if(oListTechFlockUsers.isEmpty())
                {
                    return BaseResult.success("操作成功，但群成员被清空，没法通知其他成员");
                }
                for(TechFlockUserEntity oTechFlockUserEntityInner : oListTechFlockUsers)
                {
                    MemberVo oMemberVo = new MemberVo();
                    oMemberVo.setId(oTechFlockUserEntityInner.getUserId());
                    oMemberVo.setUserName(oTechFlockUserEntityInner.getUserName());
                    oMemberVo.setPhone(oTechFlockUserEntityInner.getMobilePhone());
                    list.add(oMemberVo);
                }
            }
            oIMqttProcessService.userRevokeFlockTalkRecord(recordId, oYxcscFlockTalkRecordEntity.getFlockId(),oYxcscFlockTalkRecordEntity.getFlockType(),list);
            return BaseResult.success(list);
        //}        
        //return BaseResult.success();
    }
    /*** 
    * 客服查询某个服务中自己所处理的会话接口
    * @param sessionId  服务Id
    * @param customerId  客服Id
    * @return Page
    **/
    @Override
    public BaseResult queryCustomerAccessService(String sessionId,Long customerId)
    {
        QueryWrapper<YxcscCustomerAccessServiceEntity> oCustomerAccessServiceQueryWrapper = new QueryWrapper();
        oCustomerAccessServiceQueryWrapper.eq("session_id",sessionId);
        oCustomerAccessServiceQueryWrapper.eq("cust_id",customerId);
        return BaseResult.success(oYxcscCustomerAccessServiceMapper.selectList(oCustomerAccessServiceQueryWrapper));    
    }
    /*** 
    * 客服根据会话Id查询聊天记录接口
    * @param sessionId  服务Id
    * @param customerId  客服Id
    * @return Page
    **/
    @Override
    public BaseResult queryServiceTalkRecordByCustomerAccessIds(int currentPage,int pageSize,List<YxcscCustomerAccessServiceEntity> oListCustomerAccessService)
    {
        if(pageSize > 50)
        {
            return BaseResult.failed("分页大小不能超过50条记录");
        }
        
        if(oListCustomerAccessService.isEmpty())
        {
            return BaseResult.failed("记录Id不能为空");
        }
        Page<YxcscServiceTalkRecordEntity> pageParams = new Page<>(currentPage,pageSize);
        QueryWrapper<YxcscServiceTalkRecordEntity> oServiceTalkQueryWrapper = new QueryWrapper();
        if(oListCustomerAccessService.size() == 1)
        {
            oServiceTalkQueryWrapper.eq("access_service_id",oListCustomerAccessService.get(0).getId());
        }
        else
        {
            //oServiceTalkQueryWrapper.eq("access_service_id",AccessIds.get(0).getId());
            for(int i=0;i<oListCustomerAccessService.size();i++)
            {
                oServiceTalkQueryWrapper.or().eq("access_service_id",oListCustomerAccessService.get(i).getId());
            }
        }
        oServiceTalkQueryWrapper.orderByDesc("create_time");
        //oYxcscServiceTalkRecordMapper.selectPage(pageParams, oServiceTalkQueryWrapper);
        return BaseResult.success(oYxcscServiceTalkRecordMapper.selectPage(pageParams,oServiceTalkQueryWrapper));    
    }
    /*** 
    * 获取日志文件列表
    * @return BaseResult
    **/
    @Override
    public BaseResult getErrorLogFiles(String LogFilePath)
    {
        String rootDir ;
        if(StrUtil.isBlank(LogFilePath))
        {
            rootDir = com.yxw.yxnet_cd_center.customer_service.CustomerServiceApplication.getAppCurrenDir();
        }
        else
        {
            rootDir = LogFilePath.trim();
        }
        if(StrUtil.isBlank(rootDir))
        {
            return BaseResult.failed("获取日志根目录为空");
        }
        File rootFile = new File(rootDir);
        if(!rootFile.exists())
        {
            return BaseResult.failed("获取日志根目录失败,当前目录信息:"+ rootDir);
        }
        JSONObject rootDirJson = new JSONObject();
        rootDirJson.put("name", rootDir);
        JSONArray rootDirList = new JSONArray();
        File[] fileList = rootFile.listFiles();
        if(null != fileList)
        for(File file : fileList)
        {
            JSONObject fileJson = new JSONObject();
            fileJson.put("name", file.getName());
            fileJson.put("path", file.getPath());
            try
            {
                fileJson.put("URLEncoderPath", java.net.URLEncoder.encode(file.getPath(), "UTF-8"));
            }
            catch(UnsupportedEncodingException oE)
            {
                System.out.println("getErrorLogFiles Exception:" + oE.getMessage());
            } 
            if(file.isFile())
            {
                fileJson.put("type", "file");
            }
            else
            {
                fileJson.put("type", "path");
                JSONArray filesJsonArray = new JSONArray();
                File[] files = file.listFiles();
                if(null != files)
                for(File fileInner : files)
                {
                    JSONObject fileInnerJson = new JSONObject();
                    fileInnerJson.put("name", fileInner.getName());
                    fileInnerJson.put("path", fileInner.getPath());
                    try
                    {
                        fileInnerJson.put("URLEncoderPath", java.net.URLEncoder.encode(fileInner.getPath(), "UTF-8"));
                    }
                    catch(UnsupportedEncodingException oE)
                    {
                        System.out.println("getErrorLogFiles Exception:" + oE.getMessage());
                    }                    
                    if(fileInner.isFile())
                    {
                        fileInnerJson.put("type", "file");
                    }
                    else
                    {
                        fileInnerJson.put("type", "path");
                        fileInnerJson.put("innerFiles", fileInner.listFiles());
                    }
                    filesJsonArray.add(fileInnerJson);
                }
                fileJson.put("innerFiles", filesJsonArray);
            }
            rootDirList.add(fileJson);
        }
        rootDirJson.put("pathList", rootDirList);
        return BaseResult.success(rootDirJson);
    }    
}